import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Put,
  UseGuards,
} from '@nestjs/common';
import { DictService } from './dict.service';
import { CreateDictDto } from './dto/create-dict.dto';
import { UpdateDictDto } from './dto/update-dict.dto';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiTags,
} from '@nestjs/swagger';
import {
  ApiArrayOkResponse,
  ApiObjOkResponse,
} from '../../common/decorator/response.decorator';
import { Dict } from './entities/dict.entity';
import { CreateDictItemDto } from './dto/create-dict-item.dto';
import { DictItem } from './entities/dict-item.entity';
import { UpdateDictItemDto } from './dto/update-dict-item.dto';
import { Roles } from '../../common/decorator/roles.decorator';
import { RolesGuard } from '../../common/guards/roles.guard';
import { AuthGuard } from '@nestjs/passport';
import { Role } from '../../common/types/role';

@Controller('dict')
@ApiTags('[ADMIN]字典管理')
@ApiBearerAuth()
export class DictController {
  constructor(private readonly dictService: DictService) {}

  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ summary: '新增字典' })
  @ApiObjOkResponse(CreateDictDto)
  @Post()
  create(@Body() createDictDto: CreateDictDto) {
    return this.dictService.create(createDictDto);
  }

  @ApiOperation({ summary: '新增字典项' })
  @ApiObjOkResponse(DictItem)
  @ApiParam({ name: 'code' })
  @Post(':code/item')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  createItem(
    @Param('code') code: string,
    @Body() createDictItemDto: CreateDictItemDto,
  ) {
    return this.dictService.createItem(code, createDictItemDto);
  }

  @Get()
  @ApiOperation({ summary: '查询所有字典' })
  @ApiArrayOkResponse(CreateDictDto)
  findAll() {
    return this.dictService.findAll();
  }

  @Get(':code')
  @ApiObjOkResponse(Dict)
  @ApiOperation({ summary: '查询单个字典' })
  findOne(@Param('code') code: string) {
    return this.dictService.findOne(code);
  }

  @Put(':code')
  @ApiOperation({ summary: '更新字典' })
  @ApiObjOkResponse(CreateDictDto)
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  update(@Param('code') code: string, @Body() updateDictDto: UpdateDictDto) {
    return this.dictService.update(code, updateDictDto);
  }

  @ApiOperation({ summary: '更新字典项' })
  @ApiObjOkResponse(DictItem)
  @ApiParam({ name: 'code' })
  @ApiParam({ name: 'id' })
  @Put(':code/item/:id')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  updateItem(
    @Param('code') code: string,
    @Param('id') id: string,
    @Body() updateDictItemDto: UpdateDictItemDto,
  ) {
    return this.dictService.updateItem(id, updateDictItemDto);
  }

  @Delete(':code')
  @ApiOperation({ summary: '删除字典' })
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  remove(@Param('code') code: string) {
    return this.dictService.remove(code);
  }

  @ApiOperation({ summary: '删除字典项' })
  @ApiObjOkResponse(DictItem)
  @ApiParam({ name: 'code' })
  @ApiParam({ name: 'id' })
  @Delete(':code/item/:id')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @UseGuards(AuthGuard('jwt'))
  removeItem(@Param('code') code: string, @Param('id') id: string) {
    return this.dictService.removeItem(id);
  }
}
